home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 4 / QRZ Ham Radio Callsign Database - Volume 4.iso / files / tcpip / amiga / asrc29p.lha / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  2.5 KB  |  100 lines

  1. /*
  2. **    @(#)getopt.c    2.2 (smail) 1/26/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  * 
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24.  
  25. #include "global.h"
  26. #define index strchr
  27. #ifdef BSD
  28. #include <strings.h>
  29. #else
  30. #include <string.h>
  31. #endif
  32.  
  33. #include <fcntl.h>
  34.  
  35. /*LINTLIBRARY*/
  36. #ifndef    NULL
  37. #define NULL    0
  38. #endif
  39. #define EOF    (-1)
  40. #define ERR(s, c)    if(opterr){\
  41.     extern int write();\
  42.     char errbuf[2];\
  43.     errbuf[0] = c; errbuf[1] = '\n';\
  44.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  45.     (void) write(2, s, (unsigned)strlen(s));\
  46.     (void) write(2, errbuf, 2);}
  47.  
  48. extern char *index();
  49.  
  50. int    opterr = 1;
  51. int    optind = 1;
  52. int    optopt;
  53. char    *optarg;
  54.  
  55. int
  56. getopt(argc, argv, opts)
  57. int    argc;
  58. char    **argv, *opts;
  59. {
  60.     static int sp = 1;
  61.     register int c;
  62.     register char *cp;
  63.  
  64.     if(sp == 1)
  65.         if(optind >= argc ||
  66.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  67.             return(EOF);
  68.         else if(strcmp(argv[optind], "--") == 0) {
  69.             optind++;
  70.             return(EOF);
  71.         }
  72.     optopt = c = argv[optind][sp];
  73.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  74.         ERR(": illegal option -- ", c);
  75.         if(argv[optind][++sp] == '\0') {
  76.             optind++;
  77.             sp = 1;
  78.         }
  79.         return('?');
  80.     }
  81.     if(*++cp == ':') {
  82.         if(argv[optind][sp+1] != '\0')
  83.             optarg = &argv[optind++][sp+1];
  84.         else if(++optind >= argc) {
  85.             ERR(": option requires an argument -- ", c);
  86.             sp = 1;
  87.             return('?');
  88.         } else
  89.             optarg = argv[optind++];
  90.         sp = 1;
  91.     } else {
  92.         if(argv[optind][++sp] == '\0') {
  93.             sp = 1;
  94.             optind++;
  95.         }
  96.         optarg = NULL;
  97.     }
  98.     return(c);
  99. }
  100.